package sample.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JdbcUtils
{

    /**
     * 获取数据库连
     * */
    public static Connection getConnection(String url, String username, String password, String DriverName)
    {
        Connection con = null;
        try
        {
            Class.forName(DriverName);
            con = DriverManager.getConnection(url, username, password);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 关闭连接
     * */
    public static void close(ResultSet rs, Statement stm, Connection con)
    {
        try
        {
            if (rs != null)
            {
                rs.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            if (stm != null)
            {
                rs.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            if (con != null)
            {
                rs.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
